using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GameJamKingdom
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public SoundLibrary sbank;

        public Dictionary<string, State> stateDict;
        public string cState;        

        public bool keyFlag;
        public string keyName;

        public int finalScore;

        public Game1()
        {
            finalScore = 0;
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
        
        protected override void Initialize()
        {
            base.Initialize();
            
            keyFlag = false;

            graphics.PreferredBackBufferWidth = Program.wWidth;
            graphics.PreferredBackBufferHeight = Program.wHeight;

            graphics.ApplyChanges();

            sbank = new SoundLibrary(this);
            

            stateDict = new Dictionary<string, State>();
            stateDict.Add("menu", new MenuState(this));
            stateDict.Add("gameplay", new GameplayState(this));
            stateDict.Add("gameover", new GameOverState(this));
            stateDict.Add("credits", new CreditsState(this));
            stateDict.Add("tutorial", new TutorialScreen(this));

            cState = "menu";
            
            // initialize the first state
            stateDict[cState].Initialize(this);

            Song song = Content.Load<Song>("Sounds/SnakeRunnerLoop02");
            MediaPlayer.Volume = 0.05f;
            MediaPlayer.IsRepeating = true;
            MediaPlayer.Play(song);


            
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // update based on the current state
            stateDict[cState].Update(this, gameTime);


            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            // draw based on current state
            stateDict[cState].Draw(spriteBatch);

            base.Draw(gameTime);
        }
    }
}
